import { type MyChatConversationResult, type MyChatConversationRquest } from "@/api/siderBar/model"
import { postMychatChats } from "@/api/siderBar/myChat"

export function useJobListFetch() {
    const pagination = ref({
        pageIndex: 0,
        pageSize: 20
    })
    const { evaluate, hasNext, list: jobList, reset } = usePaginationData<MyChatConversationResult>()
    const { trigger, dispose, indicatorRef: jobListIndicatorRef, loading, pristine } = useInfinite((args) => {
        const { pageIndex, pageSize } = toValue(pagination)
        const current = pageIndex + 1
        return postMychatChats({
            page: {
                pageIndex: current,
                pageSize
            },
            search: (args ?? {}) as MyChatConversationRquest,
            sort: {
                reverse: false
            }
        }).then(({ count, items }) => {
            evaluate({
                list: items,
                pagination: {
                    current,
                    pageSize,
                    total: count
                }
            })
            pagination.value.pageIndex = current
        })
    }, hasNext)
    onUnmounted(() => {
        dispose()
    })
    async function refetchJobList(search: MaybeRefOrGetter<Partial<MyChatConversationRquest>>) {
        pagination.value.pageIndex = 0
        reset(false)
        await trigger(toValue(search))
    }
    return {
        jobList,
        jobListIndicatorRef,
        jobListLoading: loading,
        jobListPristine: pristine,
        refetchJobList,
    }
}